package com.tz.dao.impl;

import java.util.List;

import com.tz.dao.IBookDao;
import com.tz.entity.Book;
import com.tz.entity.Category;
import com.tz.entity.Page;
import com.tz.util.HibernateCallback;
import com.tz.util.HibernateTemplate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

/**
 * Created by heziqian on 2016/10/21.
 */
@SuppressWarnings("unchecked")
@Component
public class BookDaoImpl implements IBookDao {

	

	@Override
	public Book selectBookById(final long bookId) {
		
		return (Book) HibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session ses) throws HibernateException {
				return ses.get(Book.class, bookId);
			}
		});
	}

	@Override
	public Category selectBookCategoryById(final String cateId) {
		return (Category) HibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session ses) throws HibernateException {
				return ses.get(Category.class,Long.parseLong(cateId));
			}
		});
	}

	@Override
	public List<Category> selectAllBookCategory() {
		return  (List<Category>) HibernateTemplate
				.execute(new HibernateCallback() {
					@Override
					public Object doInHibernate(Session ses)
							throws HibernateException {
						return ses.createQuery("from com.tz.entity.Category").list();
					}
				});
	}

	
	@Override
	public Page selectBookByPage(final String cateId, final int pageNow, final int pageSize) {
		
		Page pageing=new Page();
		//查询符合条件的图书集�?
				List<Book> books = (List<Book>) HibernateTemplate
						.execute(new HibernateCallback() {
							@Override
							public Object doInHibernate(Session ses)
									throws HibernateException {
								String hql = "from com.tz.entity.Book b where 1=1";
								
								if (cateId != null && !"".equals(cateId)) {
									hql += " and b.category.cateId=" + cateId;
								}
								return ses.createQuery(hql)
										.setFirstResult((pageNow - 1) * pageSize)
										.setMaxResults(pageSize).list();
							}
						});

				//获取记录的�?�行�?
				long rowCount = getRowCount(cateId);

				//计算
				long pageCount = 0;
				if (rowCount % pageSize == 0) {
					pageCount = rowCount / pageSize;
				} else {
					pageCount = rowCount / pageSize + 1;
				}

				//封装PageBean对象
				pageing.setBooks(books);
				pageing.setPageNow(pageNow);
				pageing.setPageSize(pageSize);
				pageing.setRowCount(rowCount);
				pageing.setPageCount(pageCount);

				return pageing;
	}
	
	
	/**
	 * 获取符合条件的图书记录的总数
	 */
	private Long getRowCount( final String cateId) {
		return (Long) HibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session ses) throws HibernateException {
				String hql = "select count(*) from com.tz.entity.Book b where 1=1";
				
				if (cateId != null && !"".equals(cateId)) {
					hql += " and b.category.cateId=" + Long.parseLong(cateId);
				}
				return ses.createQuery(hql).uniqueResult();
			}
		});
	}

	@Override
	public Page selectBookBystate(final String state, final int pageNow, final int pageSize) {
		Page pageing=new Page();
		//查询符合条件的图书集�?
				List<Book> books = (List<Book>) HibernateTemplate
						.execute(new HibernateCallback() {
							@Override
							public Object doInHibernate(Session ses)
									throws HibernateException {
								return ses.createQuery(" from com.tz.entity.Book b where b.state=?")
										.setParameter(0, state)
										.setFirstResult((pageNow - 1) * pageSize)
										.setMaxResults(pageSize).list();
							}
						});

				//获取记录的�?�行�?
				long rowCount = getRowCount2(state);

				//计算
				long pageCount = 0;
				if (rowCount % pageSize == 0) {
					pageCount = rowCount / pageSize;
				} else {
					pageCount = rowCount / pageSize + 1;
				}

				//封装PageBean对象
				pageing.setBooks(books);
				pageing.setPageNow(pageNow);
				pageing.setPageSize(pageSize);
				pageing.setRowCount(rowCount);
				pageing.setPageCount(pageCount);

				return pageing;
	}

	private Long getRowCount2(final String state) {
		return (Long) HibernateTemplate.execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session ses) throws HibernateException {	
				return ses.createQuery("select count(*) from com.tz.entity.Book b where b.state=?").setParameter(0, state).uniqueResult();
			}
		});
	}
}
